package com.ibeeking.found.cop.b.rest.service.job;

import com.ibeeking.found.common.constants.JobExecutorHandlerConstant;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.cop.b.rest.service.IMsgGroupMassMessageService;
import com.ibeeking.found.cop.b.rest.service.IMsgMassMessageService;
import com.ibeeking.found.cop.service.common.param.CopJobHandleParam;
import com.ibeeking.gmc.global.api.common.dto.TenantDTO;
import com.ibeeking.gmc.global.api.feign.GlobalTenantFeignClient;
import com.ibeeking.gmc.job.core.biz.model.ReturnT;
import com.ibeeking.gmc.job.core.handler.annotation.XxlJob;
import com.ibeeking.found.job.model.BaseJobModel;
import com.ibeeking.found.job.model.BusinessKeyModel;
import com.ibeeking.found.job.service.IJobService;

import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName CopJobHandler
 * @Description 订单job
 * @Author ibeeking
 * @Date 2022/4/13 11:06
 **/
@Component
public class CopJobHandler {

    @Resource
    private GlobalTenantFeignClient globalTenantFeignClient;

    @Resource
    private IJobService jobServiceImpl;

    @Resource
    private IMsgGroupMassMessageService msgGroupMassMessageServiceImpl;

    @Resource
    private IMsgMassMessageService msgMassMessageServiceImpl;

    @XxlJob(JobExecutorHandlerConstant.TENANT_COP_SEND_GROUP_MASS_MESSAGE_TO_WXCP)
    public ReturnT<String> sendGroupMassMessageToWxCpJobHandler(String param) throws Exception {
        LogUtils.info(">>>>>>>>>>>>>>执行客户消息发送任务:sendMassMessageJobHandler<<<<<<<<<<<<<<<<<<<<<<");
        //Integer tenantId = Integer.valueOf(JsonUtils.fromJson(param, BusinessKeyModel.class).getBusinessKey());
        CopJobHandleParam handleParam = JsonUtils.fromJson(param, CopJobHandleParam.class);
        if(handleParam == null) {
            LogUtils.error("无法获取群消息任务的参数");
            return ReturnT.SUCCESS;
        }
        Integer tenantId = handleParam.getTenantId();
        Long msgId = handleParam.getId();
        //判断租户是否有效
        ResponseResult<TenantDTO> responseResult = globalTenantFeignClient.queryTenantById(tenantId);
        DataUtils.trueThrowException(!ResponseCodeEnum.SUCCESS.getCode().equals(responseResult.getCode()), responseResult.getMsg());
        TenantDTO tenantDTO = responseResult.getData();
        if (null == tenantDTO){
            //无效进行任务删除
            BaseJobModel jobModel = new BaseJobModel();
            jobModel.setBusinessKey(String.valueOf(tenantId));
            jobModel.setExecutorHandler(JobExecutorHandlerConstant.TENANT_COP_SEND_GROUP_MASS_MESSAGE_TO_WXCP);
            jobModel.setType(2);
            jobServiceImpl.delJob(jobModel);
            return ReturnT.SUCCESS;
        }
        msgGroupMassMessageServiceImpl.sendGroupMassMessageToWxCp(tenantId, msgId);

        return ReturnT.SUCCESS;
    }

    @XxlJob(JobExecutorHandlerConstant.TENANT_COP_SEND_MASS_MESSAGE_TO_WXCP)
    public ReturnT<String> sendMassMessageToWxCpJobHandler(String param) throws Exception {
        LogUtils.info(">>>>>>>>>>>>>>执行客户消息发送任务:sendMassMessageJobHandler<<<<<<<<<<<<<<<<<<<<<<");
        //Integer tenantId = Integer.valueOf(JsonUtils.fromJson(param, BusinessKeyModel.class).getBusinessKey());
        CopJobHandleParam handleParam = JsonUtils.fromJson(param, CopJobHandleParam.class);
        if(handleParam == null) {
            LogUtils.error("无法获取群消息任务的参数");
            return ReturnT.SUCCESS;
        }
        Integer tenantId = handleParam.getTenantId();
        Long msgId = handleParam.getId();
        //判断租户是否有效
        ResponseResult<TenantDTO> responseResult = globalTenantFeignClient.queryTenantById(tenantId);
        DataUtils.trueThrowException(!ResponseCodeEnum.SUCCESS.getCode().equals(responseResult.getCode()), responseResult.getMsg());
        TenantDTO tenantDTO = responseResult.getData();
        if (null == tenantDTO){
            //无效进行任务删除
            //删除定时任务
            BaseJobModel jobModel = new BaseJobModel();
            jobModel.setBusinessKey(String.valueOf(tenantId));
            jobModel.setExecutorHandler(JobExecutorHandlerConstant.TENANT_COP_SEND_MASS_MESSAGE_TO_WXCP);
            jobModel.setType(2);
            jobServiceImpl.delJob(jobModel);
            return ReturnT.SUCCESS;
        }
        msgMassMessageServiceImpl.sendMassMessageToWxCp(tenantId, msgId);

        return ReturnT.SUCCESS;
    }

}
